home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP04.ZIP / CHAP04 / DKOALA / KOALA.CPP < prev    next >
C/C++ Source or Header  |  1993-04-13  |  5KB  |  239 lines

  1. /*
  2.  * KOALA.CPP
  3.  * Koala Object version 1.00
  4.  *
  5.  * Implementation of the CKoala and CImpIPersist objects that works
  6.  * in either an EXE or DLL.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "koala.h"
  19.  
  20.  
  21. /*
  22.  * CKoala::CKoala
  23.  * CKoala::~CKoala
  24.  *
  25.  * Parameters (Constructor):
  26.  *  punkOuter       LPUNKNOWN of a controlling unknown, if it exists.
  27.  *  pfnDestroy      LPFNDESTROYED to call when an object is destroyed.
  28.  */
  29.  
  30. CKoala::CKoala(LPUNKNOWN punkOuter, LPFNDESTROYED pfnDestroy)
  31.     {
  32.     m_cRef=0;
  33.     m_punkOuter=punkOuter;
  34.     m_pfnDestroy=pfnDestroy;
  35.  
  36.     //NULL any contained interfaces initially.
  37.     m_pIPersist=NULL;
  38.  
  39.     return;
  40.     }
  41.  
  42.  
  43. CKoala::~CKoala(void)
  44.     {
  45.     //Free contained interfaces.
  46.     if (NULL!=m_pIPersist)
  47.         delete m_pIPersist;     //Interface does not free itself.
  48.  
  49.     return;
  50.     }
  51.  
  52.  
  53.  
  54. /*
  55.  * CKoala::FInit
  56.  *
  57.  * Purpose:
  58.  *  Performs any intiailization of a CKoala that's prone to failure
  59.  *  that we also use internally before exposing the object outside.
  60.  *
  61.  * Parameters:
  62.  *  None
  63.  *
  64.  * Return Value:
  65.  *  BOOL            TRUE if the function is successful, FALSE otherwise.
  66.  */
  67.  
  68. BOOL CKoala::FInit(void)
  69.     {
  70.     LPUNKNOWN       pIUnknown=(LPUNKNOWN)this;
  71.  
  72.     if (NULL!=m_punkOuter)
  73.         pIUnknown=m_punkOuter;
  74.  
  75.     //Allocate contained interfaces.
  76.     m_pIPersist=new CImpIPersist(this, pIUnknown);
  77.  
  78.     return (NULL!=m_pIPersist);
  79.     }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. /*
  87.  * CKoala::QueryInterface
  88.  * CKoala::AddRef
  89.  * CKoala::Release
  90.  *
  91.  * Purpose:
  92.  *  IUnknown members for CKoala object.
  93.  */
  94.  
  95. STDMETHODIMP CKoala::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  96.     {
  97.     *ppv=NULL;
  98.  
  99.     /*
  100.      * The only calls we get here for IUnknown are either in a non-aggregated
  101.      * case or when we're created in an aggregation, so in either we always
  102.      * return our IUnknown for IID_IUnknown.
  103.      */
  104.     if (IsEqualIID(riid, IID_IUnknown))
  105.         *ppv=(LPVOID)this;
  106.  
  107.     /*
  108.      * For IPersist we return our contained interface.  For EXEs we
  109.      * have to return our interface for IPersistStorage as well since
  110.      * OLE 2.0 doesn't support IPersist implementations by themselves
  111.      * (assumed only to be a base class).  If a user asked for an
  112.      * IPersistStorage and used it, they would crash--but this is
  113.      * a demo, not a real object.
  114.      */
  115.     if (IsEqualIID(riid, IID_IPersist) || IsEqualIID(riid, IID_IPersistStorage))
  116.         *ppv=(LPVOID)m_pIPersist;
  117.  
  118.     //AddRef any interface we'll return.
  119.     if (NULL!=*ppv)
  120.         {
  121.         ((LPUNKNOWN)*ppv)->AddRef();
  122.         return NOERROR;
  123.         }
  124.  
  125.     return ResultFromScode(E_NOINTERFACE);
  126.     }
  127.  
  128.  
  129. STDMETHODIMP_(ULONG) CKoala::AddRef(void)
  130.     {
  131.     return ++m_cRef;
  132.     }
  133.  
  134.  
  135. STDMETHODIMP_(ULONG) CKoala::Release(void)
  136.     {
  137.     ULONG       cRefT;
  138.  
  139.     cRefT=--m_cRef;
  140.  
  141.     if (0==m_cRef)
  142.         {
  143.         /*
  144.          * Tell the housing that an object is going away so it can
  145.          * shut down if appropriate.
  146.          */
  147.         if (NULL!=m_pfnDestroy)
  148.             (*m_pfnDestroy)();
  149.  
  150.         delete this;
  151.         }
  152.  
  153.     return cRefT;
  154.     }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164. /*
  165.  * CImpIPersist::CImpIPersist
  166.  * CImpIPersist::~CImpIPersist
  167.  *
  168.  * Parameters (Constructor):
  169.  *  pObj            LPVOID of the object we're in.
  170.  *  punkOuter       LPUNKNOWN to which we delegate.
  171.  */
  172.  
  173. CImpIPersist::CImpIPersist(LPVOID pObj, LPUNKNOWN punkOuter)
  174.     {
  175.     m_cRef=0;
  176.     m_pObj=pObj;
  177.     m_punkOuter=punkOuter;
  178.     return;
  179.     }
  180.  
  181. CImpIPersist::~CImpIPersist(void)
  182.     {
  183.     return;
  184.     }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. /*
  192.  * CImpIPersist::QueryInterface
  193.  * CImpIPersist::AddRef
  194.  * CImpIPersist::Release
  195.  *
  196.  * Purpose:
  197.  *  IUnknown members for CImpIPersist object.
  198.  */
  199.  
  200. STDMETHODIMP CImpIPersist::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  201.     {
  202.     return m_punkOuter->QueryInterface(riid, ppv);
  203.     }
  204.  
  205.  
  206. STDMETHODIMP_(ULONG) CImpIPersist::AddRef(void)
  207.     {
  208.     ++m_cRef;
  209.     return m_punkOuter->AddRef();
  210.     }
  211.  
  212. STDMETHODIMP_(ULONG) CImpIPersist::Release(void)
  213.     {
  214.     --m_cRef;
  215.     return m_punkOuter->Release();
  216.     }
  217.  
  218.  
  219.  
  220.  
  221. /*
  222.  * CImpIPersist::GetClassID
  223.  *
  224.  * Purpose:
  225.  *  Returns the Class ID of this object.
  226.  *
  227.  * Parameters:
  228.  *  pClsID          LPCLSID in which to store our class ID.
  229.  *
  230.  * Return Value:
  231.  *  HRESULT         NOERROR always.
  232.  */
  233.  
  234. STDMETHODIMP CImpIPersist::GetClassID(LPCLSID pClsID)
  235.     {
  236.     *pClsID=CLSID_Koala;
  237.     return NOERROR;
  238.     }
  239.